<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="机构名称" prop="institutionname">
      <el-input v-model="dataForm.institutionname" placeholder="机构名称"></el-input>
    </el-form-item>
    <el-form-item label="预约联系人" prop="bookcontacts">
      <el-input v-model="dataForm.bookcontacts" placeholder="预约联系人"></el-input>
    </el-form-item>
    <el-form-item label="预约时间" prop="booktime">
      <el-input v-model="dataForm.booktime" placeholder="预约时间"></el-input>
    </el-form-item>
    <el-form-item label="预约人手机" prop="bookphonenumber">
      <el-input v-model="dataForm.bookphonenumber" placeholder="预约人手机"></el-input>
    </el-form-item>
    <el-form-item label="预约目的" prop="bookobjective">
      <el-input v-model="dataForm.bookobjective" placeholder="预约目的"></el-input>
    </el-form-item>
    <el-form-item label="预约结果" prop="bookresult">
      <el-input v-model="dataForm.bookresult" placeholder="预约结果"></el-input>
    </el-form-item>
    <el-form-item label="预约状态" prop="bookstate">
      <el-input v-model="dataForm.bookstate" placeholder="预约状态"></el-input>
    </el-form-item>
    <el-form-item label="预约设置人" prop="booksetter">
      <el-input v-model="dataForm.booksetter" placeholder="预约设置人"></el-input>
    </el-form-item>
    <el-form-item label="预约提醒" prop="bookremind">
      <el-input v-model="dataForm.bookremind" placeholder="预约提醒"></el-input>
    </el-form-item>
    <el-form-item label="是否再次预约" prop="yesnobook">
      <el-input v-model="dataForm.yesnobook" placeholder="是否再次预约"></el-input>
    </el-form-item>
    <el-form-item label="操作人" prop="operator">
      <el-input v-model="dataForm.operator" placeholder="操作人"></el-input>
    </el-form-item>
    <el-form-item label="操作日期" prop="operationtime">
      <el-input v-model="dataForm.operationtime" placeholder="操作日期"></el-input>
    </el-form-item>
    <el-form-item label="机构编号" prop="institutionid">
      <el-input v-model="dataForm.institutionid" placeholder="机构编号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          institutionname: '',
          bookcontacts: '',
          booktime: '',
          bookphonenumber: '',
          bookobjective: '',
          bookresult: '',
          bookstate: '',
          booksetter: '',
          bookremind: '',
          yesnobook: '',
          operator: '',
          operationtime: '',
          institutionid: ''
        },
        dataRule: {
          institutionname: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          bookcontacts: [
            { required: true, message: '预约联系人不能为空', trigger: 'blur' }
          ],
          booktime: [
            { required: true, message: '预约时间不能为空', trigger: 'blur' }
          ],
          bookphonenumber: [
            { required: true, message: '预约人手机不能为空', trigger: 'blur' }
          ],
          bookobjective: [
            { required: true, message: '预约目的不能为空', trigger: 'blur' }
          ],
          bookresult: [
            { required: true, message: '预约结果不能为空', trigger: 'blur' }
          ],
          bookstate: [
            { required: true, message: '预约状态不能为空', trigger: 'blur' }
          ],
          booksetter: [
            { required: true, message: '预约设置人不能为空', trigger: 'blur' }
          ],
          bookremind: [
            { required: true, message: '预约提醒不能为空', trigger: 'blur' }
          ],
          yesnobook: [
            { required: true, message: '是否再次预约不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作人不能为空', trigger: 'blur' }
          ],
          operationtime: [
            { required: true, message: '操作日期不能为空', trigger: 'blur' }
          ],
          institutionid: [
            { required: true, message: '机构编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/com/bookmanagement/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.institutionname = data.bookmanagement.institutionname
                this.dataForm.bookcontacts = data.bookmanagement.bookcontacts
                this.dataForm.booktime = data.bookmanagement.booktime
                this.dataForm.bookphonenumber = data.bookmanagement.bookphonenumber
                this.dataForm.bookobjective = data.bookmanagement.bookobjective
                this.dataForm.bookresult = data.bookmanagement.bookresult
                this.dataForm.bookstate = data.bookmanagement.bookstate
                this.dataForm.booksetter = data.bookmanagement.booksetter
                this.dataForm.bookremind = data.bookmanagement.bookremind
                this.dataForm.yesnobook = data.bookmanagement.yesnobook
                this.dataForm.operator = data.bookmanagement.operator
                this.dataForm.operationtime = data.bookmanagement.operationtime
                this.dataForm.institutionid = data.bookmanagement.institutionid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/bookmanagement/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'institutionname': this.dataForm.institutionname,
                'bookcontacts': this.dataForm.bookcontacts,
                'booktime': this.dataForm.booktime,
                'bookphonenumber': this.dataForm.bookphonenumber,
                'bookobjective': this.dataForm.bookobjective,
                'bookresult': this.dataForm.bookresult,
                'bookstate': this.dataForm.bookstate,
                'booksetter': this.dataForm.booksetter,
                'bookremind': this.dataForm.bookremind,
                'yesnobook': this.dataForm.yesnobook,
                'operator': this.dataForm.operator,
                'operationtime': this.dataForm.operationtime,
                'institutionid': this.dataForm.institutionid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
